package com.mgz.ribbon;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * RestTemplate的通讯一般在service层
 *
 * @author MiaoGuoZhu
 * @date 2018/5/4 11:02
 */
@Service
public class RibbonForwardCenterService
{

//    @Autowired
//    public LoadBalancerClient loadBalancerClient;

    @Autowired
    RestTemplate restTemplate;

    public String getName(String name) {
        //需要填写URL相对来说比较麻烦但是使用灵活
        return restTemplate.getForObject("http://client/info?name="+name,String.class);

        //垃圾写法,没有负载
//        RestTemplate restTemplate = new RestTemplate();
//        return restTemplate.getForObject("http://localhost:8074/info?name="+name,String.class);

        //感谢注解，嘻嘻嘻
//        RestTemplate restTemplate = new RestTemplate();
//        ServiceInstance client = loadBalancerClient.choose("CLIENT");
//        String url = String.format("http://%s:%s", client.getHost(), client.getPort()) +"info?name="+name;
//        return restTemplate.getForObject(url,String.class);
    }


}
